﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if DOT_NET
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
#endif
using StyleMVVM.Data;
using StyleMVVM.UnitTests.TestClasses.SimpleObjects;

namespace StyleMVVM.UnitTests.Data
{
	[TestClass]
	public class ReflectionServiceTests : BaseTestClass
	{
		[TestMethod]
		public void GetPropertyTest()
		{
			ReflectionService reflectionService = new ReflectionService();

			ExportTestInterface exportObject =
				new ExportTestInterface
					{
						A = 10,
						SimpleImport =
							new SimpleExport
								{
									TestString = "Hello",
									TestType = new SimpleType { TestInt = 40 }
								}
					};

			Assert.AreEqual(10, reflectionService.GetPropertyValue(exportObject, "A"));

			Assert.AreEqual("Hello",
			                reflectionService.GetPropertyValue(exportObject, "SimpleImport.TestString"));

			Assert.AreEqual(40,
			                reflectionService.GetPropertyValue(exportObject, "SimpleImport.TestType.TestInt"));
		}

		[TestMethod]
		public void SetPropertyTest()
		{
			ReflectionService reflectionService = new ReflectionService();

			ExportTestInterface exportObject =
				new ExportTestInterface
					{
						A = 10,
						SimpleImport =
							new SimpleExport
								{
									TestString = "Hello",
									TestType = new SimpleType { TestInt = 40 }
								}
					};

			reflectionService.SetPropertyValue(exportObject, "A", 20);
			Assert.AreEqual(20, reflectionService.GetPropertyValue(exportObject, "A"));

			reflectionService.SetPropertyValue(exportObject, "SimpleImport.TestString", "Hello Again");
			Assert.AreEqual("Hello Again",
			                reflectionService.GetPropertyValue(exportObject, "SimpleImport.TestString"));

			reflectionService.SetPropertyValue(exportObject, "SimpleImport.TestType.TestInt", 80);
			Assert.AreEqual(80,
			                reflectionService.GetPropertyValue(exportObject, "SimpleImport.TestType.TestInt"));
		}

		[TestMethod]
		public void CallMethodTest()
		{
			ReflectionService reflectionService = new ReflectionService();

			ExportTestInterface exportObject =
				new ExportTestInterface
					{
						A = 10,
						SimpleImport =
							new SimpleExport
								{
									TestString = "Hello",
									TestType = new SimpleType { TestInt = 40 }
								}
					};

			reflectionService.CallMethod(exportObject, "VoidTestMethod", true);

			reflectionService.CallMethod(exportObject, "SetAMethod", true, 2);

			Assert.AreEqual(2, exportObject.A);

			int returnValue = (int)reflectionService.CallMethod(
				exportObject, "SimpleImport.IntTestMethod", true, 50);

			Assert.AreEqual(50, returnValue);
		}
	}
}